package com.leetcode.partition1;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/3/1 21:19
 */
public class LC6Z字形变换 {

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] ss = reader.readLine().split(" ");
        writer.write(convert(ss[0], Integer.parseInt(ss[1])));
        writer.flush();
    }

    public static String convert(String s, int numRows) {
        if (numRows == 1) return s;
        int n = s.length();
        int[][] g = new int[numRows][n];
        int i = 0, x = 0, y = 0, t = 1;
        while (i < n) {
            g[x][y] = s.charAt(i++);
            x += t;
            if (x == -1) {
                t = 1;
                x = 1;
            } else if (x == numRows) {
                t = -1;
                x -= 2;
            }
            if (t == -1) y++;
        }
        StringBuilder sb = new StringBuilder();
        for (i = 0; i < numRows; i++) {
            for (int j = 0; j < s.length(); j++) {
                if (g[i][j] != 0) sb.append((char) g[i][j]);
            }
        }
        return sb.toString();
    }
}
